﻿using System;

namespace LEE.Tools.Others
{
    public static class DateTimeHelper
    {
        /// <summary>
        /// 日期转换为时间戳（时间戳单位毫秒）
        /// </summary>
        /// <returns></returns>
        public static long ToTimeStamp(this DateTime time)
        {
            var time1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return (long)(time.AddHours(-8) - time1970).TotalMilliseconds;
        }

        /// <summary>
        /// 时间戳转换为日期（时间戳单位毫秒）
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this long timeStamp)
        {
            var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return start.AddMilliseconds(timeStamp).AddHours(8);
        }

        /// <summary>
        /// 比较时间
        /// </summary>
        /// <param name="DateTime1"></param>
        /// <param name="DateTime2"></param>
        /// <returns>返回两个时间间隔小时数</returns> 
        public static int DateDiffHours(DateTime DateTime1, DateTime DateTime2)
        {
            int hours = 0;
            var ts = DateTime2 - DateTime1;
            if (ts.Days > 0)
            {
                hours += ts.Days * 24;
            }
            if (ts.Hours >= 0)
            {
                hours += ts.Minutes > 0 ? ts.Hours + 1 : ts.Hours;
            }
            return hours;
        }
    }
}
